#ifndef __PLAYBACK_INC__
#define __PLAYBACK_INC__

#define OPEN_HIGHLIGHT '<span font_family="'+song_font+'" weight="bold" size="30720" color="#FFFF00">'
#define CLOSE_HIGHLIGHT '</span>'
#define OPEN_NOHIGHLIGHT '<span font_family="'+song_font+'" weight="bold" size="30720" color="#EEEEEE">'
#define CLOSE_NOHIGHLIGHT '</span>'
#define NOHIGHLIGHT_TEXT(str) OPEN_NOHIGHLIGHT+str+CLOSE_NOHIGHLIGHT
#define HIGHLIGHT_TEXT(str) OPEN_HIGHLIGHT+str+CLOSE_HIGHLIGHT


/*------------------------------------------------------
Here is a list of processes which perform visual effects
while playing a song
------------------------------------------------------*/

// Returns the size of the graph so that it becomes as big as possible
// but still fully fits inside the screen
Function size_fullscreen(int graphic)
Private
  float scr_ratio=0.0, g_ratio=0.0;
  int graph_width=0, graph_height=0, new_size=0;

Begin
  if(graph < 0)
    return -1;    //Not gonna do that
  end;

  //Get the width/height ratio for both the graphic and the screen  
  graph_width = graphic_info(0, graphic, G_WIDTH);
  graph_height = graphic_info(0, graphic, G_HEIGHT);
  g_ratio = (float)graph_width/(float)graph_height;
  scr_ratio = resx/resy;
  if(g_ratio > scr_ratio)
    new_size = resx*100/graph_width;
  else
    new_size = resy*100/graph_height;
  end;

  return new_size;
End;

//Resize the pid.graph so it fits the screen (but still keeping its
//width/height ratio)
Function resize_fullscreen(int pid)
Begin
  if(! exists(pid))
    return -1;
  end;
  
  pid.size = size_fullscreen(pid.graph);
  return pid.size;  // It can be useful to have this info back
End;

//Resize the pid.graph so it fits the video resolution (but still keeping its
//width/height ratio)
Function resize_maxvideo(int pid)
Private
  float scr_ratio=0.0, g_ratio=0.0;
  int graph_width=0, graph_height=0;
End

Begin
  if(! exists(pid))
    return -1;    //We got the PID of a process that no longer exists
  end;

  //Get the width/height ratio for both the graphic and the screen  
  graph_width = graphic_info(0, pid.graph, G_WIDTH);
  graph_height = graphic_info(0, pid.graph, G_HEIGHT);
  g_ratio = (float)graph_width/(float)graph_height;
  scr_ratio = vresx/vresy;
  if(g_ratio > scr_ratio)
    pid.size = vresx*100/graph_width;
  else
    pid.size = vresy*100/graph_height;
  end;
End;

//This function takes a kar descriptor argument and
//displays its cover and/or background image (fading between them).
Process rotate_cover(song pointer kar, int delay)
Private
  int id1=0, id2=0, time=0, i=0, j=10;
End
  
Begin
  graph=0;
  //If both cover and background are empty, just do nothing
  if(kar.cover == "" && kar.bckgrnd == "")
    kar.cover = datadir+"/img/menu_bg.png";
    kar.bckgrnd = datadir+"/img/menu_bg.png";
  end;
  
  // Check the file exists
  if(kar.cover != "")
    if (! file_exists(kar.cover))
      kar.cover = iconv(kar.charset, "UTF8//TRANSLIT", kar.cover);
      if(! file_exists(kar.cover))
        kar.cover = datadir+"/img/menu_bg.png";
      end;
    end;
  end;
  
  if(kar.bckgrnd != "")
    if (! file_exists(kar.bckgrnd))
      kar.bckgrnd = iconv(kar.charset, "UTF8//TRANSLIT", kar.bckgrnd);
      if(! file_exists(kar.bckgrnd))
        kar.bckgrnd = datadir+"/img/menu_bg.png";
      end;
    end;
  end;

  dbg_print("DEBUG: Background: '"+kar.bckgrnd+"'");
  dbg_print("DEBUG: Cover:      '"+kar.cover+"'");

  /* IMPORTANT: If we only have one image to show, only one os these IF blocks*/
  /*            is executed, otherwise the code loops below                   */
  if( (kar.bckgrnd == "" && kar.cover != "") ||
      (kar.cover == "" && kar.bckgrnd != "") )
    
    // Swap bg <-> cover in case kar.cover == ""
    if(kar.cover == "")
      kar.cover = kar.bckgrnd;
      kar.bckgrnd = "";
    end;

    //In this case, there's only one image, display cover statically
    if(is_png(kar.cover))
      graph = load_png(kar.cover);
    else
      graph = load_image(kar.cover);
    end;
    //Resize the image so it fits the screen
    resize_fullscreen(id);

    //Position the video in the center of the window
    x = resx/2;
    y = resy/2;
    z = 255;

    //Wait for our time to die
    while(die == 0)
      FRAME;
    end;
    unload_map(0, graph);
    return;
  elif(kar.cover == "" && kar.bckgrnd != "")
    //In this case, there's only one image, display background statically
    if(is_png(kar.bckgrnd))
      graph = load_png(kar.bckgrnd);
    else
      graph = load_image(kar.bckgrnd);
    end;

    //Resize the image so it fits the screen
    resize_fullscreen(id);

    //Position the video in the center of the window
    x = resx/2;
    y = resy/2;
    z = 255;

    //Wait for our time to die
    while(die == 0)
      FRAME;
    end;
    unload_map(0, graph);
    return;
  end;
  
  //Create both robot processes, will guide them from this one
  id1 = robot();
  if(is_png(kar.cover))
    id1.graph = load_png(kar.cover);
  else
    id1.graph = load_image(kar.cover);
  end;
  id1.alpha=255;
  
  id2 = robot();
  if(is_png(kar.bckgrnd))
    id2.graph = load_png(kar.bckgrnd);
  else
    id2.graph = load_image(kar.bckgrnd);
  end;
  id2.alpha=0;
  
  //Resize the video so it fits the screen
  resize_fullscreen(id1);
  //Position the video in the center of the window
  id1.x = resx/2;
  id1.y = resy/2;
  id1.z = 255;

  //Resize the video so it fits the screen
  resize_fullscreen(id2);
  //Position the video in the center of the window
  id2.x = resx/2;
  id2.y = resy/2;
  id2.z = 255;

  //Now we're sure we've got both a cover and a BG image  
  time = timer[8];
  while(die == 0)
    if(timer[8] > time+delay)
      dbg_print("Rotating cover @ time="+timer[8]+"cs");
      for(i=0; i<=255/abs(j); i++)
        id1.alpha-=j;
        id2.alpha+=j;
        dbg_print("Fading "+i+" id1.alpha="+id1.alpha+" id2.alpha="+id2.alpha);
        FRAME;
      end;
      
      dbg_print("Finished fading");
      
      // Restart the time count & restart the alpha values & revert the
      // fade effect for the next time it is executed
      if (id1.alpha > 255) id1.alpha = 255; end;
      if (id2.alpha > 255) id2.alpha = 255; end;
      if (id1.alpha < 0 )  id1.alpha = 0;   end;
      if (id2.alpha < 0 )  id2.alpha = 0;   end; 
      time = timer[8];
      j = -j;
    end;
    FRAME;
  end;
  
  // Tell the two processes to die, and unload their graphs
  id1.die = 1;
  if (id1.graph != 0)
    unload_map(0, id1.graph);
    id1.graph = 0;
  end;
  id2.die = 1;
  if(id2.graph != 0)
    unload_map(0, id2.graph);
    id2.graph = 0;
  end;
  return;
End;

//The bar in the bottom of the screen behind the text
Process bottom_bar()
Private
  int tmp_graph=0, tmp=0, decoration_up_pid=0, decoration_down_pid=0;
  int decoration_up_vx=0, decoration_down_vx=0;
  int decoration_up_maxvx=50, decoration_down_maxvx=35;

Begin
  // Dirty hack to calculate the bar height
  tmp_graph = pango_render('<span font_family="'+song_font+'" weight="bold" '+
    'size="30720"> </span>');
  tmp = 2*graphic_info(0, tmp_graph, G_HEIGHT);
  
  // Draw the bar background & set its alpha value & center
  graph = map_new(resx, tmp, BPP);
  drawing_map(0, graph);
  drawing_color(rgb(50, 50, 50));
  draw_box(0, 0, resx, tmp);
  alpha=180;
  set_point(0, graph, 0, resx, tmp);
  
  // Place the bar @ its location and over most of the stuff on-screen
  x=resx;
  y=resy;
  z=-10;
  
  //Create the two robot processes' y value and assign them their graphs
  decoration_up_pid = robot();
  decoration_up_pid.graph = load_png(datadir+"img/bar_decoration-up.png");
  decoration_down_pid = robot();
  decoration_down_pid.graph = load_png(datadir+"img/bar_decoration-down.png");
  decoration_up_pid.y = decoration_down_pid.y = resy-tmp;
  
  // Place them, in order, over the rest of the things onscreen
  decoration_down_pid.z = -11;
  decoration_up_pid.z = -12;
  
  // Apply some transparency
  decoration_down_pid.alpha = decoration_up_pid.alpha = 220;
  
  // Resize the decorations to fit the screen
  tmp = graphic_info(0, decoration_down_pid.graph, G_WIDTH);
  decoration_down_pid.size = decoration_up_pid.size = resx*102/tmp;

  // Hide the decoration at the right of the screen
  decoration_up_pid.x = decoration_down_pid.x = resx+tmp/2;

  // Move the decorations to their place and wait for death
  while(die==0)
    decoration_down_vx = decoration_down_pid.x - resx/2;
    decoration_up_vx = decoration_up_pid.x - resx/2;
    if(decoration_down_vx > decoration_down_maxvx)
      decoration_down_vx = decoration_down_maxvx;
    end;
    if(decoration_up_vx > decoration_up_maxvx)
      decoration_up_vx = decoration_up_maxvx;
    end;
    decoration_down_pid.x -= decoration_down_vx;
    decoration_up_pid.x -= decoration_up_vx;
    FRAME;
  End;
  
  // Unload stuff and die
  unload_map(0, graph);
  unload_map(0, decoration_up_pid.graph); decoration_up_pid.graph = 0;
  unload_map(0, decoration_down_pid.graph); decoration_down_pid.graph = 0;
  decoration_up_pid.die = decoration_down_pid.die = 1;
End

// The song announcer (a text with the song and artist name)
// The text is rendered and then displayed for a period of time
Process announce_song(string song, string author, string license)
Private
  int graph1=0, graph2=0, graph3=0, time=0, delay=500, l_gap=40, lic_pid=0;
  int shadow_pid=0, lic_shadow_pid=0;
  int graph1_width=0, graph1_height=0;
  int graph2_width=0, graph2_height=0;
  float shadow_dx=0.0, shadow_dy = 0.0;
  
Begin
  //Write the info to 2 temporary maps
  graph1 = pango_render('<span font_family="'+song_font+'" weight="bold" '+
          'color="#EEEE00" size="xx-large">'+song+'</span>');
  graph2 = pango_render('<span font_family="'+song_font+'" weight="bold" '+
          'size="large">'+author+'</span>');

  graph1_width = graphic_info(0, graph1, G_WIDTH);
  graph1_height = graphic_info(0, graph1, G_HEIGHT);
  graph2_width = graphic_info(0, graph2, G_WIDTH);
  graph2_height = graphic_info(0, graph2, G_HEIGHT);
  // If music is licensed as CC, we also display the license logo
  if(substr(license, 0, 2) == "CC" && file_exists(datadir+"img/"+license+".png"))
    graph3 = load_image(datadir+"img/"+license+".png");
  end;
  //Create a new graphic which will keep the text
  if(graph1_width >= graph2_width)
    graph = map_new(graph1_width+l_gap, graph1_height+graph2_height, BPP);
    graph1_width += l_gap;
  else
    graph = map_new(graph2_width+l_gap, graph1_height+graph2_height, BPP);
    graph1_width = graph2_width+l_gap;
  end;
  drawing_map(0, graph);
  set_point(0, graph, 0, graph1_width, 0);
  //And copy that info to the main graphic
  if(graph3 != 0)
    lic_pid = robot();
    lic_pid.x = 15+graphic_info(0, graph3, G_WIDTH)/2;
    lic_pid.y = 15+graphic_info(0, graph3, G_HEIGHT)/2;
    lic_pid.z = -5;
    lic_pid.graph = graph3;
    
    // The shadow
    lic_shadow_pid = robot();
    lic_shadow_pid.x = lic_pid.x; lic_shadow_pid.y = lic_pid.y;
    lic_shadow_pid.z = -4;
    lic_shadow_pid.graph = lic_pid.graph; //graph3
  end;
  map_block_copy(0, graph, 5, 5, graph1, 0, 0, graph1_width, graph1_height,
                 B_NOCOLORKEY);
  map_block_copy(0, graph, 5, 10+graph2_height, graph2, 0, 0, graph2_width,
                 graph2_height, B_NOCOLORKEY);
  unload_map(0, graph1);
  unload_map(0, graph2);
  // Create the shadow(s)
  shadow_pid = robot();
  shadow_pid.graph = graph;
  
  // Position the announcer
  x = resx;
  y = 0;
  z = -5; shadow_pid.z = -4;
  shadow_pid.alpha = alpha - 60;
  if(lic_shadow_pid != 0)
    lic_shadow_pid.alpha = alpha - 60;
  end;
  time = timer[8];
  //Frame and fade it
  while(die == 0)
    // Randomly move the shadows
    shadow_dx = rand(-10, 10);
    shadow_dy = rand(-10, 10);
    shadow_pid.x = x+shadow_dx;
    shadow_pid.y = y+shadow_dy;
    if(lic_shadow_pid != 0)
      lic_shadow_pid.x = lic_pid.x+shadow_dx;
      lic_shadow_pid.y = lic_pid.y+shadow_dy;
    end;
    // Must hide the announcer & license, then quit
    if(timer[8] > time+delay && alpha > 25)
      die = 1;
    end;
    FRAME(150);
  end;
  
  // Check if we should die inmediately, or can disappear gracefully
  if(die != 2)
    // Make everything disappear gracefully
    while( x<resx+graph1_width )
      // Randomly move the shadows
      shadow_dx = rand(-10, 10);
      shadow_dy = rand(-10, 10);
      shadow_pid.x = x+shadow_dx;
      shadow_pid.y = y+shadow_dy;
      if(lic_shadow_pid != 0)
        lic_shadow_pid.x = lic_pid.x+shadow_dx;
        lic_shadow_pid.y = lic_pid.y+shadow_dy;
      end;
      x+=3;
      alpha-=10;
      shadow_pid.alpha=alpha-60;
      if(lic_pid != 0)
        lic_pid.alpha = alpha;
        lic_shadow_pid.alpha = alpha - 60;
      end;
      FRAME;
    end;
  end;
  shadow_pid.graph=0;
  shadow_pid.die=0;
  unload_map(0, graph);
  if(lic_pid != 0)
    unload_map(0, lic_pid.graph);
    lic_pid.graph=0;
    lic_shadow_pid.graph=0;
    lic_shadow_pid.die=0;
    lic_pid.die=1;
  end;
  return;
End;

/*
Process display_lyrics_line(lyrics_line l)
Private
  int i=0, j=0, width=0, visibility=0, reg_orig_x=0, reg_width=0;
  string txt_bfr="", txt_aftr="";
  
Begin
  // Don't do anything if the line is blank
  if(l.line_text == "")
    return 0;
  end;

  while(die == 0)
    x = (resx - width) / 2;
    // Trigger for the line to appear as second line
    if(timer[9] > l.appearance_time && visibility == 0)
      if(l.pid != 0)
        signal(l.pid, S_WAKEUP);
      end;
      y = 0.95*resy;
      visibility = 1;
    end;
    // Trigger for the line to become the main one
    if(timer[9] > l.activation_time && visibility == 1)
      y = 0.87*resy;
      visibility = 2;
    end;
    // Trigger for the line to disappear
    if(timer[9] > l.deactivation_time && visibility == 2)
      y = 1.2*resy;
      die = 1;
    end;
    if(visibility == 2)
      // Run through the syllables to know if we must modify the line
      for(i=0; i<=syllableslimit; i++)
        if((! l.syllables[i].activated) &&
           (l.syllables[i].activation_time != 0) &&
           (timer[9] > l.syllables[i].activation_time))
          txt_bfr = txt_aftr = "";
          dbg_print("i: "+i);
          for(j=0; j<i; j++)
            txt_bfr = txt_bfr+l.syllables[j].text;
          end;
          for(j=i; j<=syllableslimit; j++)
            txt_aftr = txt_aftr+l.syllables[j].text;
          end;
          dbg_print("txt_bfr:  "+txt_bfr);
          dbg_print("txt_aftr: "+txt_aftr);
          l.syllables[i].activated = 1;
        end;
      end;
    end;
#ifdef DEBUG
    if(key(_c))
      dbg_print("DEBUG: Forcing centering");
      x = resx/2;
      y = resy/2;
    end;
#endif
    FRAME;
  end;
End;*/

/*******************************************************************
 This process is in charge of displaying, updating and removing
 the lyrics on-screen.
*******************************************************************/
Process display_lyrics(song pointer kar)
Private
  int i=0, j=0, l1_pid=0, l2_pid=0, aux_pid=0, highlight_count=0;
  string old_txt="", new_txt="";
  
Begin
	if(kar == 0)
		return -1;
	end;

  // We create the dummy processes for the two lines of text & center'em
  l1_pid = robot(); l2_pid = robot(); aux_pid = robot();
  l1_pid.x = l2_pid.x = aux_pid.x = resx / 2;
  l1_pid.z = l2_pid.z = aux_pid.z = -255;
  
  dbg_print("Timer[9]="+timer[9]);
  dbg_print("lines[0] activates @ timer[9]="+kar.lines[0].activation_time);

  while(die == 0)
    // Check wether we should refresh the graph of the currently promoted line
    new_txt = "";
    if( timer[9] < kar.lines[i-2].deactivation_time &&
        kar.lines[i-2].visibility == 2 )
        for (j=0; j<=SYLLABLESLIMIT; j++)
          if(timer[9] >= kar.lines[i-2].syllables[j].activation_time)
            new_txt += HIGHLIGHT_TEXT(kar.lines[i-2].syllables[j].text);
          else
            new_txt += NOHIGHLIGHT_TEXT(kar.lines[i-2].syllables[j].text);
          end;
        end;

        // Now, if the text has changed from the previous pass, rerender
        // the lyrics line
        if(new_txt != old_txt)
          l1_pid.graph = 0;
          unload_map(0, l1_pid.graph);
          
          dbg_print("Rendering "+new_txt);
          l1_pid.graph = pango_render(new_txt);
          
          old_txt = new_txt;
        end;
    end;
    // Check wether we should unload the graphs from previous lines
    if( timer[9] >= kar.lines[i-2].deactivation_time &&
        kar.lines[i-2].visibility == 2 )
        // Fade the text out, as speedy as possible
        aux_pid.y = l1_pid.y; aux_pid.graph = l1_pid.graph;
        dbg_print("Unloading graphic "+aux_pid.graph);
        l1_pid.graph = 0;
        
        // The first line must disappear, let's check if we've got time to
        // fade off the line before the next lyrics line promotes
        if(kar.lines[i-1].activation_time > timer[9] + 255/(50*MAIN_FPS) )
          while(aux_pid.alpha > 50)
            dbg_print("aux_pid.alpha="+aux_pid.alpha);
            aux_pid.alpha -= 50;
            FRAME;
          end;
        end;
        // Unload the graph
        unload_map(0, aux_pid.graph);
        aux_pid.graph = 0;
        aux_pid.alpha = 255;
        // Mark the line as unusable
        kar.lines[i-2].visibility = 3;
        dbg_print("Unloaded "+kar.lines[i-2].line_text);
    end;
    
    // When activation_time is passed, then the line should be promoted
    if( timer[9] > kar.lines[i-1].activation_time &&
        kar.lines[i-1].visibility == 1 )
        // First, unload the old map
        unload_map (0, l2_pid.graph);
        l2_pid.graph = 0;
        // Now, render the new one
        l1_pid.graph = pango_render (
            NOHIGHLIGHT_TEXT(kar.lines[i-1].line_text)
        );
        dbg_print("Promoting: "+kar.lines[i-1].line_text);
        l1_pid.y = l2_pid.y - graphic_info(0, l1_pid.graph, G_HEIGHT);
        kar.lines[i-1].visibility = 2;  // Text has been promoted, start highlighting
    end;
    
    // line "i" should appear on the second row
    if( (kar.lines[i].line_text != "") &&
        (timer[9] > kar.lines[i].appearance_time) &&
        (kar.lines[i].visibility == 0) )
      l2_pid.graph = pango_render (
        '<span font_family="'+song_font+'" weight="bold" size="20480" color="#DDDDDD">'+
        kar.lines[i].line_text+
        '</span>'
        );
      l2_pid.y = resy-0.5*graphic_info(0, l2_pid.graph, G_HEIGHT);
      kar.lines[i].visibility = 1;  // The text is being displayed
      dbg_print("Rendered: "+kar.lines[i].line_text);
      i++; // The line counter should only increase when a new line shows up
    end;
    
    // Exit if we've reached the maximum number of lines
    if(i >= lineslimit)
      dbg_print("Reached lineslimit while playing file");
      l1_pid.die = l2_pid.die = aux_pid.die = die = 1;
    end;
    
    FRAME;
  End;
  
  // Kill the lines, if they're active
  if(exists(l1_pid))
  	unload_map(0, l1_pid);
  	l1_pid.graph = 0;
  	l1_pid.die = 1;
 	end;
  if(exists(l2_pid))
  	unload_map(0, l2_pid);
  	l2_pid.graph = 0;
  	l2_pid.die = 1;
 	end;
End;

//The main process in charge of parsing and playing the karaoke file
Process play_file(string dirname, string filename)
Private
  int videoid=0, musicid=0, time=0, done=0, text_paused=0, audio_paused=0;
  int pid=0, bar_id=0, lyrics_pid=0, i=0;
  song kar;
  string olddir;
  
Begin
  //Don't start playing any song if any song is already playing
  //TODO: this should scan all the processes of a TYPE, not just once
  if(get_id(TYPE play_file) != id)
  	dbg_print("There's another instance of play_file running, quitting");
    return -1;
  end;

  //Parse some data from the description text file
  if(! file_exists(dirname+"/"+filename))
    dbg_print("File "+dirname+filename+" doesn't seem to exist");
    return -1;
  end;

  olddir = cd();
  chdir(dirname);

  //Get the basic stuff on the descriptor file
  desc_file_finished=0;    //Just in case
  parse_file(filename, &kar);
  //Restart the timer
  timer[9] = 0;
  ReadBeatFile(&kar);
  announce_song(kar.title, kar.artist, kar.license);
  lyrics_pid = display_lyrics(&kar);
  
  //Unload the karaoke file descriptor
  if(kar.fd != 0)
    fclose(kar.fd);
    kar.fd=0;
  end;

  //Load video and audio
  if(kar.video_file != "")
    #ifndef NO_SMPEG
      // If the file doesn't exist, try converting it to UTF8
      // as most modern FSs are UTF8, otherwise fail
      if(! file_exists(kar.video_file))
        kar.video_file = iconv(kar.charset, "UTF8//TRANSLIT", kar.video_file);
        if(! file_exists(kar.video_file))
          kar.video_file = "";
        end;
      end;
      videoid = load_mpeg(kar.video_file, 1, 0);
      if(videoid == -1)
        dbg_print("DEBUG: Couldn't load video file!!! Will try to use cover file instead");
        rotate_cover(&kar, 1500);
        videoid=0;
        else
          graph = play_mpeg(videoid);
          resize_maxvideo(id);
          x=resx/2;
          y=resy/2;
        end;
    #endif
  else
    rotate_cover(&kar, 1500);
  end;

  bar_id = bottom_bar();

  // Check the music file exists, otherwise try converting it to UTF8
  if(! file_exists(kar.music_file))
    kar.music_file = iconv(kar.charset, "UTF8//TRANSLIT", kar.music_file);
    if(! file_exists(kar.music_file))
      // Try to die gracefully
      die=1;
    end;
  end;
  fmodex_load_song(cd()+"/"+kar.music_file);
  fmodex_play_song();
  
  while( (! key(_esc)) && (fmodex_is_playing_song()) && (die==0) )
    if(timer[9] > kar.gap && audio_paused == 1)
      text_paused = 0;
    end;
    FRAME;
  end;
  
  die = 1;

  fade(200, 200, 200, 15);
  while(fading)
    FRAME;
  end;

  //Tell the bottom bar to die
  bar_id.die=1;
  lyrics_pid.die=1;
  FRAME;  //And let it die gracefully before let_me_alone()

  //Unload stuff (if needed)
  if(fmodex_is_playing_song())
    fmodex_stop_song();
	fmodex_unload_song();
  end;

  #ifndef NO_SMPEG
    if( (videoid != 0) && (is_playing_mpeg(videoid)) )
      stop_mpeg(videoid);
      unload_mpeg(videoid);
    end;
  #endif
  
  //Reset the cwd to the old one
  chdir(olddir);
  
  //Clean the screen
  let_me_alone();
  delete_text(all_text);
  clear_screen();
  graph=0;
  
  menu();
  fade(100, 100, 100, 15);
  while(fading)
    FRAME;
  end;

End;

#endif
